summaryrefslogtreecommitdiffhomepage
path: root/packages/console/app/src/routes/workspace/[id]/billing/payment-section.tsx
blob: 6da5c42ed0cc49dfb0a44cbc8947b8a18899b2e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Billing } from "@opencode-ai/console-core/billing.js"
import { query, action, useParams, createAsync, useAction } from "@solidjs/router"
import { For, Match, Show, Switch } from "solid-js"
import { withActor } from "~/context/auth.withActor"
import { formatDateUTC, formatDateForTable } from "../../common"
import styles from "./payment-section.module.css"
import { useI18n } from "~/context/i18n"

function money(amount: number, currency?: string) {
  const formatter =
    currency === "inr"
      ? new Intl.NumberFormat("en-IN", { style: "currency", currency: "INR" })
      : new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })
  return formatter.format(amount / 100_000_000)
}

const getPaymentsInfo = query(async (workspaceID: string) => {
  "use server"
  return withActor(async () => {
    return await Billing.payments()
  }, workspaceID)
}, "payment.list")

const downloadReceipt = action(async (workspaceID: string, paymentID: string) => {
  "use server"
  return withActor(() => Billing.generateReceiptUrl({ paymentID }), workspaceID)
}, "receipt.download")

export function PaymentSection() {
  const params = useParams()
  const i18n = useI18n()
  const payments = createAsync(() => getPaymentsInfo(params.id!))
  const downloadReceiptAction = useAction(downloadReceipt)

  // DUMMY DATA FOR TESTING
  // const payments = () => [
  //   {
  //     id: "pi_3QK1x2FT9vXn4A6r1234567890",
  //     paymentID: "pi_3QK1x2FT9vXn4A6r1234567890",
  //     timeCreated: new Date(Date.now() - 86400000 * 1).toISOString(), // 1 day ago
  //     amount: 2100000000, // $21.00 ($20 + $1 fee)
  //   },
  //   {
  //     id: "pi_3QJ8k7FT9vXn4A6r0987654321",
  //     paymentID: "pi_3QJ8k7FT9vXn4A6r0987654321",
  //     timeCreated: new Date(Date.now() - 86400000 * 15).toISOString(), // 15 days ago
  //     amount: 2100000000, // $21.00
  //   },
  //   {
  //     id: "pi_3QI5m1FT9vXn4A6r5678901234",
  //     paymentID: "pi_3QI5m1FT9vXn4A6r5678901234",
  //     timeCreated: new Date(Date.now() - 86400000 * 32).toISOString(), // 32 days ago
  //     amount: 2100000000, // $21.00
  //   },
  //   {
  //     id: "pi_3QH2n9FT9vXn4A6r3456789012",
  //     paymentID: "pi_3QH2n9FT9vXn4A6r3456789012",
  //     timeCreated: new Date(Date.now() - 86400000 * 47).toISOString(), // 47 days ago
  //     amount: 2100000000, // $21.00
  //   },
  //   {
  //     id: "pi_3QG7p4FT9vXn4A6r7890123456",
  //     paymentID: "pi_3QG7p4FT9vXn4A6r7890123456",
  //     timeCreated: new Date(Date.now() - 86400000 * 63).toISOString(), // 63 days ago
  //     amount: 2100000000, // $21.00
  //   },
  // ]

  return (
    <Show when={payments() && payments()!.length > 0}>
      <section class={styles.root}>
        <div data-slot="section-title">
          <h2>{i18n.t("workspace.payments.title")}</h2>
          <p>{i18n.t("workspace.payments.subtitle")}</p>
        </div>
        <div data-slot="payments-table">
          <table data-slot="payments-table-element">
            <thead>
              <tr>
                <th>{i18n.t("workspace.payments.table.date")}</th>
                <th>{i18n.t("workspace.payments.table.paymentId")}</th>
                <th>{i18n.t("workspace.payments.table.amount")}</th>
                <th>{i18n.t("workspace.payments.table.receipt")}</th>
              </tr>
            </thead>
            <tbody>
              <For each={payments()!}>
                {(payment) => {
                  const date = new Date(payment.timeCreated)
                  const amount =
                    payment.enrichment?.type === "subscription" && payment.enrichment.couponID ? 0 : payment.amount
                  const currency =
                    payment.enrichment?.type === "subscription" || payment.enrichment?.type === "lite"
                      ? payment.enrichment.currency
                      : undefined
                  return (
                    <tr>
                      <td data-slot="payment-date" title={formatDateUTC(date)}>
                        {formatDateForTable(date)}
                      </td>
                      <td data-slot="payment-id">{payment.id}</td>
                      <td data-slot="payment-amount" data-refunded={!!payment.timeRefunded}>
                        {money(amount, currency)}
                        <Switch>
                          <Match when={payment.enrichment?.type === "credit"}>
                            {" "}
                            ({i18n.t("workspace.payments.type.credit")})
                          </Match>
                          <Match when={payment.enrichment?.type === "subscription"}>
                            ({i18n.t("workspace.payments.type.subscription")})
                          </Match>
                        </Switch>
                      </td>
                      <td data-slot="payment-receipt">
                        {payment.paymentID ? (
                          <button
                            onClick={async () => {
                              const receiptUrl = await downloadReceiptAction(params.id!, payment.paymentID!)
                              if (receiptUrl) {
                                window.open(receiptUrl, "_blank")
                              }
                            }}
                            data-slot="receipt-button"
                          >
                            {i18n.t("workspace.payments.view")}
                          </button>
                        ) : (
                          <span>-</span>
                        )}
                      </td>
                    </tr>
                  )
                }}
              </For>
            </tbody>
          </table>
        </div>
      </section>
    </Show>
  )
}